package cn.techaction.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.techaction.common.SverResponse;
import cn.techaction.pojo.User;
import cn.techaction.service.ActionUserService;
import cn.techaction.utils.ConstUtil;
import cn.techaction.vo.ActionUserVo;

@Controller
@RequestMapping("/user")
public class ActionUserController {
	/**
	 *  张玉12/31日完成用户管理模块接口
	 */
	
	/*1.用户登录*/
	@Autowired//省略了setget方法
	private ActionUserService actionUserService;
	
	@RequestMapping("/do_login.do")
	@ResponseBody
	public SverResponse<User> do_Login(HttpSession session,String account,String password) {
		//1.调用Sevice层的方法：登录
		System.out.println("controller="+account);
		SverResponse<User> response=actionUserService.doLogin(account, password);
		//2.判断是否能登录
		if(response.isSuccess()) {
			//登录成功，将用户信息存入session
			//3.能登录则判断是否是管理员，是管理员存放在session，否则错误信息
			User user=response.getData();
			session.setAttribute(ConstUtil.CUR_USER, user);

			
		}
		return response;
	}
	
	/*2.用户注销登出接口*/
	@RequestMapping("/do_logout.do")
	@ResponseBody
	public SverResponse<String> do_logout(HttpSession session) {
		
		session.removeAttribute(ConstUtil.CUR_USER);
			
		
		return SverResponse.createRespBySuccess();
		
	}
	/*3.用户注册*/
	@RequestMapping("/do_register.do")
	@ResponseBody
	public SverResponse<String> registerUser(User user){
		System.out.println(user.getAsw());
		return actionUserService.doRegister(user);
	}
	
	/*4.用户个人资料修改*/
	@RequestMapping("/updateuserinfo.do")
	@ResponseBody
	public SverResponse<User> updateUserInfo(HttpSession session,ActionUserVo userVo){
		User curUser=(User) session.getAttribute(ConstUtil.CUR_USER);//当前页面登陆的用户
		if(curUser==null) {
			return SverResponse.createByErrorMessage("用户尚未登录！");
		}
		userVo.setId(curUser.getId());
		userVo.setAccount(curUser.getAccount());
		SverResponse<User> resp=actionUserService.updateUserInfo(userVo);
		if(resp.isSuccess()) {
			//重写session
			session.setAttribute(ConstUtil.CUR_USER,resp.getData());
		}
		return resp;
		
	}
	/*5.获取用户对象*/
	@RequestMapping("/getUserByAccount.do")
	@ResponseBody
	//验证用户，获取用户对象
	public SverResponse<User> getUserbyAccount(String account) {
		return actionUserService.findUserByAccount(account);
		
	}
	
	/*6.检查密保答案是否一致*/
	@RequestMapping("/checkuserasw.do")
	@ResponseBody
	//验证用户密保答案
	public SverResponse<String> checkUserAnswer(String account,String question,String asw) {
		
		return actionUserService.checkUserAnswer(account,question,asw);
	}
	
	/*7.根据密保问题和密保答案重置密码*/
	@RequestMapping("/resetpassword.do")
	@ResponseBody
	
	public SverResponse<String> resetPassword(Integer userId,String newpwd) {
		
		return actionUserService.resetPassword(userId,newpwd);
	}

	@RequestMapping("/do_check_info.do")
	@ResponseBody
	public SverResponse<String> doCheckInfo(String info,String type){
		//将我们的session取出
			return actionUserService.checkValidation(info, type);
		
	}
	
	/*8.获取用户信息接口*/
	@RequestMapping("/getuserinfo.do")
	@ResponseBody
	//验证用户密保答案
	public SverResponse<User> getUserInfo(HttpSession session) {
		User curUser=(User) session.getAttribute(ConstUtil.CUR_USER);
		if(curUser==null) {
			return SverResponse.createByErrorMessage("用户尚未登录！");
		}
		return SverResponse.createRespBySuccess(curUser);
	}
	
	/*9.修改密码接口*/
	@RequestMapping("/updatepassword.do")
	@ResponseBody
	public SverResponse<String> updatePassword(HttpSession session,String oldpwd,String newpwd){
		//将我们的session取出
		User user=(User) session.getAttribute(ConstUtil.CUR_USER);
		if(user==null) {
			return SverResponse.createByErrorMessage("请登录后再修改密码！");
		}
		SverResponse<String> result=actionUserService.updatePassword(user,oldpwd,newpwd);
		//修改之后清空session，准备重新登录
		if(result.isSuccess()) {
			session.removeAttribute(ConstUtil.CUR_USER);
		}
		return result;
	}
}
